/**
 * Created by benz on 2017/3/19.
 */
var jwt = require('jsonwebtoken');
module.exports = app => {
    return class AuthController extends app.Controller {
        * login(ctx) {
            console.log(ctx.request.body);
            const userName = ctx.request.body.userName;
            const passWord = ctx.request.body.passWord;
            const user=yield ctx.model.User.findOne({where:{userName:userName}});//查用户名
            if(user){
                console.log(user.passWord);
                console.log(passWord);
                if(user.passWord===passWord){//密码匹配
                    var token = jwt.sign({ id: user.id },ctx.locals.tokenScrect);
                    this.ctx.body={
                        token:token
                    }
                }else {
                    this.ctx.status=402;
                    this.ctx.body={
                        error:"用户名或密码不匹配，请重新输入"
                    };
                }
            }else{
                this.ctx.status=402;
                this.ctx.body={
                    error:"用户名或密码不匹配，请重新输入"
                };
            }
        }
    }
}